package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.common.OperateCode;
import com.hunttown.mes.common.utils.JSONUtils;
import com.hunttown.mes.manage.service.TransportLoginWxManageService;
import com.hunttown.mes.rpc.domain.TransportLoginWxDTO;
import com.hunttown.mes.rpc.domain.enums.api.CustomerEnum;
import com.hunttown.mes.rpc.domain.query.TransportLoginWxDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * @title: 微信用户
 * @author: wangjunfu
 * @date: 2019-09-17 13:27:17
 * @description:
 */
@Controller
@RequestMapping(value = "/basic/loginwx")
public class LoginWxController {

    private final static Logger logger = LoggerFactory.getLogger(LoginWxController.class);

    private final TransportLoginWxManageService loginWxService;

    @Autowired
    public LoginWxController(TransportLoginWxManageService loginWxService){
        this.loginWxService = loginWxService;
    }

    /**
     * 首页
     *
     * @param model    model模型
     * @param dtoQuery 查询条件
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, TransportLoginWxDTOQuery dtoQuery) {

        if (dtoQuery.getHasMobile() == null) {
            dtoQuery.setHasMobile(0);
        }

        dtoQuery.setOrderBy("order by id desc");
        Page<TransportLoginWxDTO> dataPage = loginWxService.getForPage(dtoQuery);

        // 实时更新列表信息
        if (dataPage != null && dataPage.getItems().size() > 0) {
            for (TransportLoginWxDTO item : dataPage.getItems()) {
                item.setUserTypeName(CustomerEnum.getEnumByKey(item.getUserType()));
            }
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);

        model.addAttribute("customerList", CustomerEnum.getEnumList());

        return "basic/loginwx/index";
    }

    @RequestMapping(value = "/relieveBindMobile", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> relieveBindMobile(HttpServletRequest request, Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("returnCode", 0);

        try {
            //取消绑定即：将当前手机号清空
            TransportLoginWxDTO update = new TransportLoginWxDTO();
            update.setId(id);
            update.setMobile("");
            Boolean isNext = loginWxService.updateInfoById(update);

            if (isNext) {
                map.put("returnCode", 1);
                map.put("returnMessage", "手机号解绑成功！");
            } else {
                map.put("returnMessage", "手机号解绑失败！");
            }
        } catch (Exception e) {
            //清空map
            map.clear();
            map.put("returnMessage", e.getMessage());
        }

        return map;
    }

    @RequestMapping(value = "/changeFlag", method = RequestMethod.POST)
    @ResponseBody
    public String openOrStopFlag(Integer id, Integer status) {

        // 结果返回 结果至少包含一个操作状态和一条状态信息
        String result = "";
        Map<String, String> map = new HashMap<String, String>();

        try {
            Boolean isNext = loginWxService.changeState(id, status);
            map = OperateCode.CompositionResult(map, isNext ? 1 : 0);
            result = JSONUtils.toJSON(map);
            return result;

        } catch (Exception e) {
            //清空map
            map.clear();
            map = OperateCode.CompositionResult(map, -1);
            result = JSONUtils.toJSON(map);
            return result;
        }
    }

}